using System;
using System.IO;
using CustomControls;
using System.Drawing;
using System.Custom.Window;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace CoreTypes
{
   public interface ICommunicationEventObject
   {
      bool LogEvents
      {
         get;
         set;
      }
      Collection<ServiceMessage> Events
      {
         get;
      }

      event EventHandler<ErrorEventArgs> UnhandledError;
      event EventHandler<ErrorEventArgs> InvalidArgumentError;
      event EventHandler<ErrorEventArgs> ServiceOperationError;
      event EventHandler<MessagingOperationEventArgs> InvalidMessageEvent;

      event EventHandler<NetworkOperationEventArgs> LogonState;
      event EventHandler<NetworkOperationEventArgs> SeekUsersState;
      event EventHandler<NetworkOperationEventArgs> DropAccountState;
      event EventHandler<NetworkOperationEventArgs> SyncContactsState;
      event EventHandler<NetworkOperationEventArgs> CreateAccountState;
      event EventHandler<NetworkOperationEventArgs> NotifyPresenceState;
      event EventHandler<NetworkOperationEventArgs> RequestUserInfoState;
      event EventHandler<SaveAccountDataEventArgs> SaveAccountDataState;
      event EventHandler<NetworkOperationEventArgs> LoadAccountDataState;
      event EventHandler<SaveMediaSessionEventArgs> SaveMediaSessionState;
      event EventHandler<MessagingOperationEventArgs> SendInvitationState;
      event EventHandler<MessagingOperationEventArgs> SendChatMessageState;
      event EventHandler<MessagingOperationEventArgs> SendAcknowledgeState;
      event EventHandler<MessagingOperationEventArgs> UpdateChatMessageState;
      event EventHandler<MessagingOperationEventArgs> SendInvitationReplyState;

      event EventHandler<MessagingOperationEventArgs> PresenceNotified;
      event EventHandler<MessagingOperationEventArgs> InvitationReceived;
      event EventHandler<MessagingOperationEventArgs> ChatMessageReceived;
      event EventHandler<MessagingOperationEventArgs> AcknowledgeReceived;
      event EventHandler<MediaSessionUpdateEventArgs> MediaSessionUpdated;
      event EventHandler<MessagingOperationEventArgs> InvitationReplyReceived;
      event EventHandler<MessagingOperationEventArgs> ChatMessageUpdateReceived;
   }

   public interface ICommunicationEventHanldlerObject
   {
      void OnLogonState(object sender, NetworkOperationEventArgs e);
      void OnSeekUsersState(object sender, NetworkOperationEventArgs e);
      void OnDropAccountState(object sender, NetworkOperationEventArgs e);
      void OnSyncContactsState(object sender, NetworkOperationEventArgs e);
      void OnCreateAccountState(object sender, NetworkOperationEventArgs e);
      void OnSaveMediaSessionState(object sender, SaveMediaSessionEventArgs e);
      void OnNotifyPresenceState(object sender, NetworkOperationEventArgs e);
      void OnRequestUserInfoState(object sender, NetworkOperationEventArgs e);
      void OnSaveAccountDataState(object sender, SaveAccountDataEventArgs e);
      void OnLoadAccountDataState(object sender, NetworkOperationEventArgs e);
      void OnSendInvitationState(object sender, MessagingOperationEventArgs e);
      void OnSendChatMessageState(object sender, MessagingOperationEventArgs e);
      void OnSendAcknowledgeState(object sender, MessagingOperationEventArgs e);
      void OnUpdateChatMessageState(object sender, MessagingOperationEventArgs e);
      void OnSendInvitationReplyState(object sender, MessagingOperationEventArgs e);

      void OnPresenceNotified(object sender, MessagingOperationEventArgs e);
      void OnInvitationReceived(object sender, MessagingOperationEventArgs e);
      void OnChatMessageReceived(object sender, MessagingOperationEventArgs e);
      void OnAcknowledgeReceived(object sender, MessagingOperationEventArgs e);
      void OnMediaSessionUpdated(object sender, MediaSessionUpdateEventArgs e);
      void OnInvitationReplyReceived(object sender, MessagingOperationEventArgs e);
      void OnChatMessageUpdateReceived(object sender, MessagingOperationEventArgs e);
   }

   public interface ICommunicationServiceObject : ICommunicationEventObject
   {
      int ListenPort
      {
         get;
      }
      IBaseCommunicator Communicator
      {
         get;
      }

      void Logoff(Account account);
      void DropAccount(Account account);
      void SyncContacts(Account account);
      void LoadAccountData(Account account);
      void CreateAccount(RegistrationInfo info);
      void SendChatMessage(ChatMessage message);
      void UpdateMessage(ServiceMessage message);
      void Logon(Account account, Status status);
      void SeekUsers(string query, IWindow sender);
      void SendAcknowledge(ServiceMessage message);
      void SendInvitation(SessionInvitation message);
      void RequestUserInfo(User user, bool shortInfo);
      void SendInvitationReply(ServiceMessage message);
      void NotifyPresence(Account account, User user, Status status);
      void ChangePrivateData(Account account, string password, string mail);
      void SaveAccountData(Account account, SaveAccountActions actions, params object[] data);
      void SaveMediaSession(IMediaSession session, SaveMediaSessionActions actions, params object[] data);
      void SetPermissions(Account account, object target, PolicyRule message, PolicyRule invitation, PolicyRule visibility);

      bool IsConnected(Account account);
      bool IsLogonInProgress(Account account);
      ushort AddListener(string name, ushort port);
   }

   public interface IBaseCommunicator : ICommunicationServiceObject
   {
      string AppName
      {
         get;
      }

      string ProxyType
      {
         get;
         set;
      }
      string ProxyHost
      {
         get;
         set;
      }
      string ProxyPort
      {
         get;
         set;
      }
      string ProxyUsername
      {
         get;
         set;
      }
      string ProxyPassword
      {
         get;
         set;
      }

      EventLog EventLog
      {
         get;
      }
      int InstanceNumber
      {
         get;
      }
      AccountSet Accounts
      {
         get;
      }
      ColorTheme ColorTheme
      {
         get;
         set;
      }
      IMainWindow MainWindow
      {
         get;
      }
      Account CurrentAccount
      {
         get;
         set;
      }
      string SelectedLogStyle
      {
         get;
      }
      string SelectedMessageTemplate
      {
         get;
      }
      ToolStripRenderMode RenderMode
      {
         get;
         set;
      }
      SortedList<string, Image> ContactsViewImages
      {
         get;
      }
      Dictionary<Type, MediaSessionTypeInfo> SupportedMediaSessionTypes
      {
         get;
      }

      event PropertyChangedEventHandler PropertyChanged;

      void OptimizeMemorySize();
      void NormalizeMessage(ServiceMessage message);
      void Log(Exception error, bool save);
      ContactGroup AddContactGroup(string name, string parent, Account owner);

      void ShowCssEditWindow();
      void ShowUserMenu(User user);
      void ShowSettings(string page);
      void ShowRegisterAccount(ServiceConfig config);
      void ShowError(string message, Exception error);

      IMediaSession CreateMediaSession(string typeName,
                                       Account account,
                                       Contact owner,
                                       SessionVisibility visibility,
                                       ServiceSession hostingSession,
                                       SessionSubtype subtype);
      IMediaSession CreateMediaSession(MediaSessionTypeInfo typeInfo,
                                       Account owner,
                                       IEnumerable<User> invitedUsers,
                                       SessionVisibility visibility,
                                       SessionHostingType hostingType,
                                       ServiceSession hostingSession,
                                       SessionSubtype subtype);

      void DrawTheme(ColorTheme theme, Graphics g, Rectangle r, ColorThemeObject t);
   }

   public interface IMainWindow : IWindow, ICommunicationEventHanldlerObject
   {
      bool CanBeDeactivated
      {
         get;
      }

      ItemView GetContactsView(Account owner);
   }

   public interface IMediaWindow : IWindow, ICommunicationEventHanldlerObject
   {
      string SearchString
      {
         get;
      }
      bool TextSearchVisible
      {
         get;
      }
      bool ExclusiveSessionMode
      {
         get;
      }
      IMediaSession ActiveMediaSession
      {
         get;
         set;
      }

      void HideTextSearch();
      void ShowTextSearch(bool filtered);
      void AttachSession(IMediaSession session);
      void DetachSession(IMediaSession session);
      void ShowAddParticipant(IMediaSession session);
      void ComposeReply(ChatMessage message, string reply);
   }

   public interface IRegisterWidget
   {
      bool IsFinished { get; }
      
      event EventHandler Started;
      event EventHandler Faulted;
      event EventHandler Progress;
      event EventHandler Finished;
      event EventHandler NextAllowed;
      event EventHandler NextBlocked;
      
      bool Next();
      void Back();
      void Start();
   }
}